<?php

define('PHPLIB_INCLUDED', true);

function isInt($val) {
    return is_int($val) || (string)(int)$val === (string)$val;
}

function isNumeric($val) {
	return isInt($val) || (string)(float)$val === (string)$val;
}

function emptyNotNull($val) {
    return $val === null ? false : empty($val);
}

function emptyNotZero($val) {
    return !$val && (string)(int)$val === (string)$val ? false : empty($val);
}

function isTimestamp($t) {
	return (string)$t === date('U', (int)$t);
}

function isNotNull($val) {
	return $val !== null;
}

function isEmpty($val, $inDepth = true) {
	if (empty($val)) {
		return true;
	} elseif (is_array($val) && $inDepth) {
		foreach ($val as $v) {
			if (!isEmpty($v)) {
				return false;
			}
		}
		return true;
	} else {
		return false;
	}
}

function isNotEmpty($val) {
	return !isEmpty($val);
}

function zerofill($val, $l) {
	return isInt($val) ? str_pad((string)$val, $l, '0', STR_PAD_LEFT) : '';
}

function unzerofill($val) {
	return (int)$val;
}

function stripNotDigit($val) {
    return preg_replace("/[^\d]+/", "", $val);
}

function stripDigit($val) {
	return preg_replace("/[\d]+/", "", $val);
}

function arrayFilter(array& $val, $callback = null, $rmEmptyArr = false) {
	foreach ($val as $k => $v) {
		if (is_array($v)) {
			arrayFilter($val[$k], $callback, $rmEmptyArr);

			if ($rmEmptyArr && empty($val[$k])) {
				unset($val[$k]);
			}
		} elseif ( (null !== $callback && !$callback($v)) || empty($v)) {
			unset($val[$k]);
		}
	}
}

function arrayDiffKey(array $a1, array $a2, $rmEmptyArr = false) {
	$res = array();
	foreach ($a1 as $k => $v) {
		if (array_key_exists($k, $a2)) {
			if (is_array($v) && is_array($a2[$k])) {
				$res[$k] = arrayDiffKey($v, $a2[$k], $rmEmptyArr);

				if ($rmEmptyArr && empty($res[$k])) {
					unset($res[$k]);
				}
			}
			continue;
		}
		$res[$k] = $v;
	}
	return $res;
}

function arrayIntersectKey(array $a1, array $a2, $rmEmptyArr = false) {
	$res = array();
	foreach ($a1 as $k => $v) {
		if (array_key_exists($k, $a2)) {
			if (is_array($v) && is_array($a2[$k])) {
				$res[$k] = arrayIntersectKey($v, $a2[$k], $rmEmptyArr);
				
				if ($rmEmptyArr && empty($res[$k])) {
					unset($res[$k]);
				}
			} else {
				$res[$k] = $v;
			}
		}
	}
	return $res;
}

function arrayMerge(array $a1, array $a2) {
    $res = array();
    foreach ($a1 as $k => $v) {
        if (array_key_exists($k, $a2)) {
           if (is_array($v)) {
               $res[$k] = arrayMerge($v, (array)$a2[$k]);
           } else {
               $res[$k] = $a2[$k];
           }
           continue;
        }
        $res[$k] = $v;
    }

    foreach ($a2 as $k => $v) {
        if (!array_key_exists($k, $a1)) {
            $res[$k] = $v;
        }
    }
    return $res;
}

function arrayStraight(array $array, $maxDepth = -1, $depth = 0) {
	$res = array();

	foreach ($array as $k => $v) {
		if (!is_array($v) || ($maxDepth >= 0 && $depth == $maxDepth) ) {
			$res[] = $v;
		} else {
			$res = array_merge($res, arrayStraight($v, $maxDepth, $depth + 1));
		}
	}

	return $res;
}

function timestampToDatetime($value) {
   if (isInt($value)) {
       return date('Y-m-d H:i:s', $value);
   }
}

function timestampToDate($value) {
   if (isInt($value)) {
       return date('Y-m-d', $value);
   } 
}

function hexToRgb($hex) {
    $hex = (int)hexdec($hex);
    return array(
        $hex >> 16 & 255,
        $hex >> 8 & 255,
        $hex  & 255
    );
}

//detect image type
//@see http://ru2.php.net/manual/en/function.exif-imagetype.php
if (!function_exists('exif_imagetype')) {
    function exif_imagetype($filename) {
        if ((list($width, $height, $type, $attr) = getimagesize($filename)) !== false) {
            if ($type > 0) {
                return $type;
            }
        }
        return false;
    }
}

function dump($var, $logType = null) {
	if (is_string($logType)) {
		switch ($logType) {
			case 'e':
			case 'error':
				$logType = Zend_Log::ERR;
			break;

			case 'w':
			case 'warning':
				$logType = Zend_Log::WARN;
			break;

			case 'n':
			case 'notice':
				$logType = Zend_Log::NOTICE;
			break;

			case 'i':
			case 'info':
				$logType = Zend_Log::INFO;
			break;

			case 'd':
			case 'debug':
				$logType = Zend_Log::DEBUG;

		}
	}

	if ($logType === null) {
		if (class_exists('Zend_Debug')) {
			Zend_Debug::dump($var);
		} else {
			echo '<pre>'; var_dump($var); echo '</pre>';
		}
	}
	elseif ($logType == 'print_r') {
		echo '<pre>'; print_r($var); echo '</pre>';
	}
	elseif ($log = Zend_Registry::get('logger')) {
		$log->log($var, $logType);
	}
}

function exprTest($expr, $type = null, $msg = null, $logType = 'print_r') {
	if (null !== $type) {
		$var = $expr;
		settype($var, $type);
		$expr = $var;
	}

	if (null !== $msg) {
		$expr = "$msg: ".var_export($expr, true);
	}

	dump($expr, $logType);
}
